package com.lfg.leetcode.leetcode0;


import java.util.Arrays;

public class No45Jump2 {

    public static void main(String[] args) {

        int ret = jump(new int[]{2, 3, 1, 1, 4});
//        int ret2 = jump2(new int[]{0});
        System.out.println(ret);
    }

    public static int jump(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int step = 0;
        int endIndex = 0;
        int maxDistance = 0;

        for (int i = 0; i < nums.length-1; i++) {
            maxDistance = Math.max(maxDistance,  i + nums[i]);
            if(i == endIndex) {
                endIndex  = maxDistance;
                step++;
            }

        }
        return step;
    }

}

